package com.feifan.web.controller.oa;

import com.feifan.common.annotation.Log;
import com.feifan.common.core.controller.BaseController;
import com.feifan.common.core.domain.AjaxResult;
import com.feifan.common.core.page.TableDataInfo;
import com.feifan.common.enums.BusinessType;
import com.feifan.common.utils.poi.ExcelUtil;
import com.feifan.oa.domain.Holiday;
import com.feifan.oa.domain.HolidayDate;
import com.feifan.oa.service.IHolidayDateService;
import com.feifan.oa.service.IHolidayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 假期名称Controller
 * 
 * @author 非凡
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/oa/holiday")
public class HolidayController extends BaseController
{
    private String prefix = "oa/vacation/holiday";

    @Autowired
    private IHolidayService holidayService;

    @Autowired
    private IHolidayDateService holidayDateService;

    @RequiresPermissions("oa:holiday:view")
    @GetMapping()
    public String holiday()
    {
        return prefix + "/holiday";
    }

    /**
     * 查询假期名称列表
     */
    @RequiresPermissions("oa:holiday:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Holiday holiday)
    {
        startPage();
        List<Holiday> list = holidayService.selectHolidayList(holiday);
        return getDataTable(list);
    }

    /**
     * 导出假期名称列表
     */
    @RequiresPermissions("oa:holiday:export")
    @Log(title = "假期名称", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Holiday holiday)
    {
        List<Holiday> list = holidayService.selectHolidayList(holiday);
        ExcelUtil<Holiday> util = new ExcelUtil<Holiday>(Holiday.class);
        return util.exportExcel(list, "holiday");
    }

    /**
     * 新增假期名称
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存假期名称
     */
    @RequiresPermissions("oa:holiday:add")
    @Log(title = "假期名称", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Holiday holiday)
    {
        return toAjax(holidayService.insertHoliday(holiday));
    }

    /**
     * 修改假期名称
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Holiday holiday = holidayService.selectHolidayById(id);
        mmap.put("holiday", holiday);
        return prefix + "/edit";
    }

    /**
     * 修改保存假期名称
     */
    @RequiresPermissions("oa:holiday:edit")
    @Log(title = "假期名称", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Holiday holiday)
    {
        return toAjax(holidayService.updateHoliday(holiday));
    }

    /**
     * 删除假期名称
     */
    @RequiresPermissions("oa:holiday:remove")
    @Log(title = "假期名称", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        HolidayDate holidayDates = holidayDateService.selectHolidayDateByHId(Long.parseLong(ids));
        if (holidayDates != null) {
            return AjaxResult.error("假期名称下有假期时间，不能删除！");
        }
        return toAjax(holidayService.deleteHolidayByIds(ids));
    }
}
